<%@ page language="java"
		 import="com.ruohi.gift.order.OrderStatus,com.ruohi.util.common.CommonApplication,com.ruohi.util.common.CommonForm"
		 pageEncoding="UTF-8" %>
<%@ page import="com.ruohi.util.common.CommonString" %>
<%@ page import="com.ruohi.util.common.Env" %>
<%@ page import="java.util.LinkedHashMap" %>
<%@ page import="java.util.Map" %>
<%@ page import="java.util.TreeMap" %>
<%
	String tableName = "gift_order";
	String tablePrefix = Env.getInstance().getProperty("table_prefix");
	String fileds = "id,uuid,add_date,order_num,order_type,mem_no,receive_name,product_total_price,ship_total_price,order_status,pay_mode,sms_alert_send,offline_pay_remark,ship_remark,pay_gateway_no,is_partner_payment,invoice_title,(select back_enable_operate from gift_order_status_operate where order_status=gift_order.order_status) as statusoperate";
	String channelUUID = CommonString.getFormatPara(request.getParameter("channel_uuid"));
	String whereStr = " ";
	String orderByStr = " add_date desc ";
	
	Map orderStatusMap=new TreeMap();
	orderStatusMap.put("","-请选择-");
	orderStatusMap.put(CommonString.getFormatPara(OrderStatus.CANCELED),OrderStatus.showStatus(OrderStatus.CANCELED));
	orderStatusMap.put(CommonString.getFormatPara(OrderStatus.WAIT_PAY),OrderStatus.showStatus(OrderStatus.WAIT_PAY));
	orderStatusMap.put(CommonString.getFormatPara(OrderStatus.PAYED),OrderStatus.showStatus(OrderStatus.PAYED));
	orderStatusMap.put(CommonString.getFormatPara(OrderStatus.ASSORTING),OrderStatus.showStatus(OrderStatus.ASSORTING));
	orderStatusMap.put(CommonString.getFormatPara(OrderStatus.SHIPPED),OrderStatus.showStatus(OrderStatus.SHIPPED));
	orderStatusMap.put(CommonString.getFormatPara(OrderStatus.FINISHED),OrderStatus.showStatus(OrderStatus.FINISHED));
	orderStatusMap.put(CommonString.getFormatPara(OrderStatus.REFUND),OrderStatus.showStatus(OrderStatus.REFUND));
	
	Map orderTypeMap = new TreeMap();
	orderTypeMap.put("", "-请选择-");
	orderTypeMap.put("0", "普通订单");
	orderTypeMap.put("1", "拍卖订单");
	orderTypeMap.put("2", "团购订单");

	Map payModeMap = new LinkedHashMap();
	payModeMap.put("", "-请选择-");
	payModeMap.putAll(CommonApplication.getEnumChildren("168", application));
%><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
  <head>
    <title><%=Env.getInstance().getProperty("project_name") %></title>
	<link href="/plugin/ui/ligerUI/ligerUI/skins/Aqua/css/ligerui-all.css" rel="stylesheet" type="text/css" />
	<link href="/plugin/ui/ligerUI/ligerUI/skins/Gray/css/all.css" rel="stylesheet" type="text/css" />
	<link href="/plugin/ui/ligerUI/ligerUI/skins/ligerui-icons.css" rel="stylesheet" type="text/css" />
	<link href="/backend/style/style.css" rel="stylesheet" type="text/css" />
	<script src="/plugin/jquery/jquery.min.js" type="text/javascript"></script>
  </head>
  <body>
  	<div class="searchForm">
  		<div class="searchCondition">
  			<div class="searchList">
		  		<div class="searchLeft">订单号</div>
		  		<div class="searchRight">
		  			<input type="text" class="jr_text" name="order_num" id="order_num" />
		  		</div>
		  	</div>
  			<div class="searchList">
		  		<div class="searchLeft">收货人</div>
		  		<div class="searchRight">
		  			<input type="text" class="jr_text" name="receive_name" id="receive_name" />
		  		</div>
		  	</div>
		  	<div class="searchList" style="width:440px;">
		  		<div class="searchLeft">下单时间</div>
		  		<div class="searchRight" style="width:320px;">
		  			<input type="text" class="Wdate jr_text" style="width:120px;" onclick="WdatePicker({dateFmt:'yyyy-MM-dd'});" name="add_date_start" id="add_date_start" />
		  			-
		  			<input type="text" class="Wdate jr_text" style="width:120px;" onclick="WdatePicker({dateFmt:'yyyy-MM-dd'});" name="add_date_end" id="add_date_end" />
		  		</div>
		  	</div>
		  	<div class="searchList">
		  		<div class="searchLeft">订单状态</div>
		  		<div class="searchRight">
		  			<select name="order_status" id="order_status" class="jr_select">
		  				<%=CommonForm.createSelect(orderStatusMap,"") %>
		  			</select>
		  		</div>
		  	</div>
			<div class="searchList">
				<div class="searchLeft">支付方式</div>
				<div class="searchRight">
					<select name="pay_mode" id="pay_mode" class="jr_select">
						<%=CommonForm.createSelect(payModeMap, "") %>
					</select>
				</div>
			</div>
			<%--<div class="searchList">--%>
			<%--<div class="searchLeft">订单类型</div>--%>
			<%--<div class="searchRight">--%>
			<%--<select name="order_type" id="order_type" class="jr_select">--%>
			<%--<%=CommonForm.createSelect(orderTypeMap, "") %>--%>
			<%--</select>--%>
			<%--</div>--%>
			<%--</div>--%>
			<%--<div class="searchLeft">是否可提现</div>--%>
			<%--<div class="searchRight">--%>
			<%--<select class="jr_select" id="is_partner_payment" name="is_partner_payment" >--%>
			<%--<option value="">--请选择--</option>--%>
			<%--<option value="1">是</option>--%>
			<%--<option value="0">否</option>--%>
			<%--</select>--%>
			<%--</div>--%>
  		</div>
  		<div class="searchBtn">
  			<input type="button" class="btn" onclick="doSearch();" value="查询" />
  			<input type="button" style="margin-left: 5px;" class="btn" onclick="doClearForm();" value="重置" />
  			<div style="clear:both;"></div>
  			<a href="javascript://void(0);" onclick="exportExcel();">按指定条件导出订单</a>
  		</div>
  	</div>
  	<div style="clear: left;"></div>
  	
	<div class="l-loading" style="display: block" id="pageloading"></div> 
	<div id="maingrid" style="margin: 0; padding: 0"></div>
	<div style="display: none;"></div>
  </body>
</html>
<script src="/plugin/ui/ligerUI/ligerUI/js/ligerui.min.js" type="text/javascript"></script>
<script src="/plugin/date/My97DatePicker/WdatePicker.js" type="text/javascript"></script>
<script src="/backend/scripts/common.js" prefix="<%=tablePrefix %>" type="text/javascript"></script>
<script type="text/javascript" src="/backend/scripts/gift_order_operate.js"></script>
<script type="text/javascript">
	var grid;
	var windowWidth = 800;
	var windowHeight = 420;
	var channelUUID = '<%=channelUUID %>';
	var tableName = "<%=tableName %>";
	var fields = "<%=fileds %>";
	var whereStr = "<%=whereStr %>";
	var orderBy = "<%=orderByStr %>";
	var columnsArr = [];
	var url = "";
	var conditionStr = "";
	var params = [];
	jQuery(function() {
		params.push({'name':'enumNo','value':'106013'});
		grid = new Dev21sunGrid({
			toolbar : {
						items : [{
									text : '删除',
									click : function(){
										toolbarDel({
											tableName:'gift_order',
											channel_uuid:'<%=channelUUID %>',
											'eventAfter':'deleteOrderProduct'
										});
									},
									icon : 'delete'
								},{
									text : '打开查询',
									click : function() {
										closeAndOpen();
									},
									icon:'right'
								}
//								,{
//									text : '可提现',
//									click : function() {
//										setPayment("1");
//									},
//									icon : 'config'
//								},{
//									text : '不可提现',
//									click : function() {
//										setPayment("0");
//									},
//									icon : 'cancelrem'
//								}
								]
					}
		});
		jQuery("#pageloading").hide();
	});
	var is_showData = [{ is_show : 1 , text: '是' }, { is_show : 0 , text: '否'}];
	//列数组
	columnsArr =  [ 
		{ display : '订单号', name : 'order_num', align : 'center' , type : 'string' , width : 130, render:function(row){
			return "<a class='link' href='javascript:void(0);' onclick='addAndEdit("+row.__index+");'>"+row.order_num+"</a>";
		} } ,
		{ display : '下单时间', name : 'add_date', align : 'center' , type : 'string', width : 160, render:function(row){
			return row.add_date.substring(0,row.add_date.length-2);
		} } ,
		//{ display:"订单类型", align:"center", type:"string", width:80, render:function(row){
		//	var str = "";
		//	switch(row.order_type){
		//		case 0:
		//			str = "普通订单";
		//		break;
		//		case 1:
		//			str = "拍卖订单";
		//		break;
		//		case 2:
		//			str = "团购订单";
		//		break;
		//		default:
		//			str = "普通订单";
		//		break;
		//	}
		//	return str;
		//} },
		{ display : '下单账号', name : 'mem_no', align : 'left', width : 160, type:'string',render:function(row){
			return "<a class='link' href='javascript:;' onclick='showMemberEdit(\""+row.mem_no+"\")'>"+row.mem_no+"</a>" ;
		} },
		{ display : '收货人', name : 'receive_name', align : 'left', width : 100, type:'string' },
		{ display : '订单金额', name : 'type', align:'right', width:100, type:'string', render:function(row){
			return (row.product_total_price+row.ship_total_price).toFixed(2);
		} },
		{ display:'订单状态', name:'order_status', align:'center', width:100, type:'string', render:function(row){
			if(row.order_status==0){
				return "已取消";
			}
			if(row.order_status==1){
				return "等待付款";
			}
			if(row.order_status==2){
				return "已付款";
			}
			if(row.order_status==3){
				return "配货中";
			}
			if(row.order_status==4){
				var sms_tip = "";
//				if(row.sms_alert_send==1){
//					sms_tip = "已发送短信提醒";
//				}else if(row.sms_alert_send==-1){
//					sms_tip = "短信提醒发送失败";
//				}else{
//					sms_tip = "未发送短信提醒";
//				}
				return "已发货(" + sms_tip + ")";
			}
			if(row.order_status==6){
				return "已退款";
			}
			return "已完成";
		} },
		{ display:'操作',name:'statusoperate',align:'center',width:180,render:function(row){
			var operateStr="";
			$.ajax({
				async:false,
				type:"post",
				url:"/backend/action/ajax_r.jsp",
				data:{flag:"gift_show_order_operate",enumNo:"106013",order_num:row.order_num,order_operate:row.statusoperate},
				success:function(result){
					operateStr=$.trim(result);
				}
			});
			return operateStr;
		} },
		{ display:'订单来源', name:'pay_gateway_no', align:'center', width:100, type:'string', render:function(row){
            if (row.pay_mode != null && $.trim(row.pay_mode) != "") {
                if (row.pay_mode == '168003') {
                    return "<span style='color:red;'>线下支付</span>";
                } else if (row.pay_mode == '168001') {
                    return "网银支付";
                } else if (row.pay_mode == '168002') {
                    if ($.trim(row.pay_gateway_no) == "WX_M") {
                        return "微信支付";
                    } else if ($.trim(row.pay_gateway_no) == "Alipay") {
                        return "支付宝";
                    } else if ($.trim(row.pay_gateway_no) == "AlipayM") {
                        return "支付宝手机端";
                    }
                }
			}
		} },
        {display: "发票抬头", name: "invoice_title", type: "string", align: "center", width: 100},
		{ display:"线下付款备注", name:"offline_pay_remark", align:"left", width:100 },
		{ display:"发货备注", name:"ship_remark", align:"left", width:100 },
		{ display:'操作记录', align:'center',width:100,render:function(row){
			return "<a href='javascript://void(0);' onclick='viewOrderOperateHistory("+row.order_num+");'>查看</a>";
		}}
	];
	
	/*列表页条件查询*/
	function doSearch(){
		params = [];
		conditionStr = "";
		var order_num = $.trim($("#order_num").val());
		if(order_num!=""){
			conditionStr += " and order_num='" + order_num + "'";
		}
		var receive_name = jQuery("#receive_name").val();
		if(''!=jQuery.trim(receive_name)){
			conditionStr += " and receive_name like '%"+receive_name+"%' ";
		}
		var start_date = jQuery("#add_date_start").val();
		if(''!=jQuery.trim(start_date)){
			conditionStr += " and add_date > '"+start_date+"' ";
		}
		var end_date = jQuery("#add_date_end").val() ;
		if(''!=jQuery.trim(end_date)){
		    conditionStr += " and convert(varchar,add_date,23)<='"+end_date+"'" ;
		}
		var state = jQuery("#order_status").val();
		if(''!=jQuery.trim(state)){
			conditionStr += " and order_status = "+state;
		}
		var type = $.trim($("#order_type").val());
		if(type != ""){
			conditionStr += " and order_type=" + type;
		}
        var pay_mode = $.trim($("#pay_mode").val());
        if (pay_mode != "") {
            conditionStr += " and pay_mode=" + pay_mode;
        }
		var is_partner_payment = $.trim(jQuery("#is_partner_payment").val());
		if(''!=jQuery.trim(is_partner_payment)){
			conditionStr += " and is_partner_payment = '"+is_partner_payment+"' ";
		}
		params.push({'name':'enumNo','value':'106013'});
		params.push({'name':'tableName','value':tableName});
		params.push({'name':'fields','value':fields});
		params.push({'name':'whereStr','value':whereStr+conditionStr});
		params.push({'name':'orderBy','value':orderBy});
		grid.options.parms=params;
		grid.loadData();
	}
	
	function showMemberEdit(memberNo){
		if(''!=jQuery.trim(memberNo)){
			jQuery.ajax({
				type:"post",
				url:"/backend/action/ajax_cud.jsp",
				data:{flag:"getMemberUuid",memberNo:memberNo},
				success:function(msg){
				if(''!=jQuery.trim(msg)){
					jrWindow('/backend/gift.21-sun.com/gift_member/gift_member_edit.jsp?channel_uuid=1&id='+jQuery.trim(msg),"",790,600,false) ;
				}
				}
			});
		}
	}
	
	function addAndEdit(index){
    	if(undefined==index){//如果是增加
			toolbarAdd({
				width:500,
				height:500,
				folderName:"gift.21-sun.com/gift_order",
				fileName:"gift_order",
				params:{
					channel_uuid:'<%=channelUUID%>'
				},
				title:"增加"
			});
    	}else{//如果是修改
	    	var row = grid.getRow(index);
	    	toolbarAdd({
	    		width:800,
	    		height:600,
	    		title:'查看订单',
	    		folderName:"gift.21-sun.com/gift_order",
				fileName:"gift_order"
	    	},row.uuid);
    	}
    }
    
    function viewOrderOperateHistory(order_num){
    	toolbarAdd({
			width:800,
			height:500,
			folderName:"gift.21-sun.com/gift_order",
			fileName:"gift_order_operate_history",
			params:{
				order_num:order_num
			},
			title:"订单操作记录"
		});
    }
    
    /**
	 * 列表删除方法
	 * 
	 * @param {}
	 *            arg 删除时要传的参数
	 */
	function toolbarDel(arg) {
		if (typeof(arg) == 'undefined' || arg == null) {
			arg = {};
		}
		var checkedRows = grid.getCheckedRows();
		if (checkedRows.length <= 0) {
			return;
		}
		// 遍历所有的选中行，如果是没有UUID，则移除
		jQuery.each(checkedRows, function(index, data) {
			if (typeof(this.uuid) == 'undefined') {
				jrDeleteRow(this.__index);
			}
		});
		jrConfirm("确定删除选中项吗？", function(v) {
			if (v) {
				var checkedRows = grid.getCheckedRows();
				var idStr = "";
				var noStr = "";
				for (var i = 0; i < checkedRows.length; i++) {
					idStr += "'" + checkedRows[i].uuid + "',";
					noStr += "'" + checkedRows[i].order_num + "',";
				}
				if (idStr.indexOf(",") != -1) {
					idStr = idStr.substring(0, idStr.length - 1);
					noStr = noStr.substring(0, noStr.length - 1);
				}
				arg.del_id = idStr;
				arg.del_order_no = noStr;
				if ('undefined' == typeof(arg.tableName)) {
					arg.tableName = tableName;
				}
				executeDel(arg);
			}
		});
	}
	
	function exportExcel(){
		var urlPara = "?enumNo=106013&flag=exportOrder";
		var start_date = jQuery("#add_date_start").val();
		if(''!=jQuery.trim(start_date)){
			urlPara += "&order_start_date=" + start_date;
		}
		var end_date = jQuery("#add_date_end").val();
		if(''!=jQuery.trim(end_date)){
		    urlPara += "&order_end_date=" + end_date;
		}
		var state = jQuery("#order_status").val();
		if(''!=jQuery.trim(state)){
			urlPara += "&order_status=" + state;
		}
		
		window.location.href="/backend/action/ajax_r.jsp" + urlPara;
	}
	
	/**
 * 批量上架方法
 * 
 * @param {}
 *            arg 推荐时要传的参数
 */
function setPayment(state) {
	var art;
	if (typeof(arg) == 'undefined' || arg == null) {
		arg = {};
	}
	var checkedRows = grid.getCheckedRows();
	if (checkedRows.length <= 0) {
		return;
	}
	// 遍历所有的选中行，如果是没有UUID，则移除
	jQuery.each(checkedRows, function(index, data) {
				if (typeof(this.uuid) == 'undefined') {
					jrDeleteRow(this.__index);
				}
			});
	var alerStr = "确定将所选择订单设置为不参加提现吗？";
	if(state=="1"){
		alerStr = "确定将所选择订单设置为参加提现吗？";
	}
	jrConfirm(alerStr, function(v) {
				if (v) {
					var checkedRows = grid.getCheckedRows();
					var idStr = "";
					for (var i = 0; i < checkedRows.length; i++) {
						idStr += "'" + checkedRows[i].uuid + "',";
					}
					if (idStr.indexOf(",") != -1) {
						idStr = idStr.substring(0, idStr.length - 1);
					}
					arg.del_id = idStr;
					if ('undefined' == typeof(arg.tableName)) {
						arg.tableName = tableName;
					}
					arg.state = state;
					executePayment(arg);
				}
			});
}

/**
 * 执行上架的方法
 * 
 * @param {}
 *            data 要传向后台的数据
 */
function executePayment(data) {
	var enumNo = '';
	for (var i = 0; i < params.length; i++) {
		if (params[i].name == 'enumNo') {
			enumNo = params[i].value;
		}
	}
	jQuery.ajax({
				url : '/backend/action/ajax_cud.jsp?enumNo=' + enumNo + '&flag=setPayment',
				data : data,
				type : 'post',
				async : false,
				cache : false,
				success : function(data) {
					grid.loadData();
				}
			});
}

</script>